package com.gidgabs.common.utils;

import com.gidgabs.common.exception.RRException;
import com.gidgabs.config.GidgabsContants;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 环信工具
 * Created by tby on 2018/7/5.
 */
public class HuanXinUtil {
    private RestTemplate restTemplate;
    /**接口地址的前半部分*/
    public static final String URL_PRE=GidgabsContants.HUAN_XIN_DNS+GidgabsContants.HUAN_XIN_CLIENT_OrgName+"/"+GidgabsContants.HUAN_XIN_CLIENT_AppName;
    /**获取token地址*/
    public static final String TOKEN="/token";
    /**注册单个用户的地址*/
    public static final String USER="/users";
    /**获取接口的凭证*/
    public static  String token;

    public HuanXinUtil(RestTemplate restTemplate){
        this.restTemplate=restTemplate;
    }
    /**
     * 获取token
     * @return
     */
    public String obtainToken(){
        Map<String,Object> params=new HashMap<>();
        params.put("client_secret", GidgabsContants.HUAN_XIN_CLIENT_SECRET);
        params.put("grant_type","client_credentials");
        params.put("client_id", GidgabsContants.HUAN_XIN_CLIENT_ID);
        Map<String,Object> result = restTemplate.postForObject(URL_PRE+TOKEN, params, Map.class);
        token= String.valueOf(result.get("access_token"));
        return token;
    }

    /**
     * 注册环信用户
     * Request Headers: {“Content-Type”:”application/json”,”Authorization”:”Bearer ${token}”}
     Request Body: {“username”:”${用户名}”,”password”:”${密码}”}
     */
    public void registHuaxin(String userId,String nickname){
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("Authorization","Bearer "+token);
        Map<String,String> params=new HashMap<>();
        params.put("username",userId);
        params.put("password",DigestUtils.sha256Hex(userId));
        params.put("nickname",nickname);
        HttpEntity<Map> requestEntity = new HttpEntity<Map>(params, httpHeaders);

        ResponseEntity<String> exchange = restTemplate.exchange(URL_PRE + USER, HttpMethod.POST, requestEntity, String.class);
        HttpStatus statusCode = exchange.getStatusCode();
        if (!"200".equals(statusCode.toString())){
            throw new RRException("注册环信用户失败,返回状态码为:"+statusCode);
        }
    }


    /**
     * 修改用户昵称
     * @param userId
     * @param nickname
     */
    public void updateNickname(String userId,String nickname){
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("Authorization","Bearer "+token);
        Map<String,String> params=new HashMap<>();
        params.put("nickname",nickname);
        HttpEntity<Map> requestEntity = new HttpEntity<Map>(params, httpHeaders);

        ResponseEntity<String> exchange = restTemplate.exchange(URL_PRE + USER+"/"+userId, HttpMethod.PUT, requestEntity, String.class);
        HttpStatus statusCode = exchange.getStatusCode();
        if (!"200".equals(statusCode.toString())){
            throw new RRException("修改环信用户昵称,返回状态码为:"+statusCode);
        }
    }
}
